﻿
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Itech_Run.Domain.Shared.Base;

namespace Itech_Run.Sys.Domain.Entity
{

    public partial class Sys_Role : CustomAggregateRoot<string>
    {

        [Required]
        [StringLength(50)]
        public string Name { get; set; }

        public bool IsActive { get; set; }

        [StringLength(200)]
        public string? Description { get; set; }

        public virtual ICollection<Sys_RoleModule> Sys_RoleModule { get; set; } = new HashSet<Sys_RoleModule>();

        public virtual ICollection<Sys_RoleModuleButton> Sys_RoleModuleButton { get; set; } = new HashSet<Sys_RoleModuleButton>();

        public virtual ICollection<Sys_UserRole> Sys_UserRole { get; } = new HashSet<Sys_UserRole>();

        #region action
        public void AllocateRoleModule(List<string> moduleList)
        {
            Sys_RoleModule = Sys_RoleModule.Where(p => moduleList.Contains(p.ModuleId)).ToList();
            moduleList = moduleList.Where(p => !Sys_RoleModule.Any(ur => ur.ModuleId == p)).ToList();

            moduleList.ForEach(item =>
            Sys_RoleModule.Add(new Sys_RoleModule
            {
                RoleId = this.Id,
                ModuleId = item,
                CreatedBy = this.LastModifiedBy,
                CreatedTime = this.LastModifiedTime,
                LastModifiedBy = this.LastModifiedBy,
                LastModifiedTime = this.LastModifiedTime
            }));
        }

        public void AllocateRoleModuleButton(List<KeyValuePair<string, string>> moduleButtonKeyValueList)
        {
            Sys_RoleModuleButton = Sys_RoleModuleButton.Where(p => moduleButtonKeyValueList.Any(mb => mb.Key == p.ModuleId && mb.Value == p.ButtonId)).ToList();
            moduleButtonKeyValueList = moduleButtonKeyValueList.Where(p => !Sys_RoleModuleButton.Any(rmb => rmb.ModuleId == p.Key && rmb.ButtonId == p.Value)).ToList();

            moduleButtonKeyValueList.ForEach(item =>
            Sys_RoleModuleButton.Add(new Sys_RoleModuleButton
            {
                RoleId = this.Id,
                ModuleId = item.Key,
                ButtonId = item.Value,
                HasAuth = true,
                CreatedBy = this.LastModifiedBy,
                CreatedTime = this.LastModifiedTime,
                LastModifiedBy = this.LastModifiedBy,
                LastModifiedTime = this.LastModifiedTime
            }));
        }
        #endregion
    }
}
